const { Singleton, LazySingleton, EagerSingleton } = require("./template.js")

function testSingletonPattern() {
  console.log("=== 单例模式测试 ===")

  console.log(
    "1. 基本单例功能:",
    (() => {
      const instance1 = Singleton.getInstance()
      const instance2 = Singleton.getInstance()

      return instance1 === instance2
    })()
  )

  console.log(
    "2. 单例数据共享:",
    (() => {
      const instance1 = Singleton.getInstance()
      const instance2 = Singleton.getInstance()

      instance1.setData("test data")
      const data = instance2.getData()

      return data === "test data"
    })()
  )

  console.log(
    "3. 懒汉式单例:",
    (() => {
      const instance1 = LazySingleton.getInstance()
      const instance2 = LazySingleton.getInstance()

      return instance1 === instance2
    })()
  )

  console.log(
    "4. 饿汉式单例:",
    (() => {
      const instance1 = EagerSingleton.getInstance()
      const instance2 = EagerSingleton.getInstance()

      return instance1 === instance2
    })()
  )

  console.log(
    "5. 多次调用getInstance:",
    (() => {
      const instances = []
      for (let i = 0; i < 5; i++) {
        instances.push(Singleton.getInstance())
      }

      // 所有实例都应该是同一个对象
      return instances.every((instance) => instance === instances[0])
    })()
  )

  console.log(
    "6. 单例状态保持:",
    (() => {
      const instance1 = Singleton.getInstance()
      instance1.setData("initial")

      const instance2 = Singleton.getInstance()
      instance2.setData("updated")

      const instance3 = Singleton.getInstance()
      const data = instance3.getData()

      return data === "updated"
    })()
  )

  console.log(
    "7. 不同单例类独立性:",
    (() => {
      const singleton1 = Singleton.getInstance()
      const lazySingleton1 = LazySingleton.getInstance()
      const eagerSingleton1 = EagerSingleton.getInstance()

      return (
        singleton1 !== lazySingleton1 &&
        singleton1 !== eagerSingleton1 &&
        lazySingleton1 !== eagerSingleton1
      )
    })()
  )

  console.log(
    "8. 单例方法调用:",
    (() => {
      const instance = Singleton.getInstance()

      instance.setData("method test")
      const result = instance.getData()

      return result === "method test"
    })()
  )

  console.log(
    "9. 懒汉式延迟初始化:",
    (() => {
      // 重置懒汉式单例（如果支持的话）
      const instance1 = LazySingleton.getInstance()
      const instance2 = LazySingleton.getInstance()

      return instance1 === instance2
    })()
  )

  console.log(
    "10. 饿汉式立即初始化:",
    (() => {
      const instance1 = EagerSingleton.getInstance()
      const instance2 = EagerSingleton.getInstance()

      return instance1 === instance2
    })()
  )

  console.log(
    "11. 单例线程安全模拟:",
    (() => {
      const instances = []

      // 模拟并发调用
      for (let i = 0; i < 10; i++) {
        instances.push(Singleton.getInstance())
      }

      // 所有实例都应该是同一个
      return instances.every((instance) => instance === instances[0])
    })()
  )

  console.log(
    "12. 单例销毁和重建:",
    (() => {
      const instance1 = Singleton.getInstance()
      instance1.setData("before")

      // 模拟销毁（如果支持的话）
      const instance2 = Singleton.getInstance()
      instance2.setData("after")

      const instance3 = Singleton.getInstance()
      const data = instance3.getData()

      return data === "after"
    })()
  )

  console.log(
    "13. this上下文测试:",
    (() => {
      const obj = {
        prefix: "singleton_",
        getSingletonData: function() {
          const instance = Singleton.getInstance()
          instance.setData(this.prefix + "test")
          return instance.getData()
        }
      }
      return obj.getSingletonData() === "singleton_test"
    })()
  )

  console.log(
    "14. 方法调用this绑定:",
    (() => {
      const obj = {
        multiplier: 2,
        processSingleton: function() {
          const instance = Singleton.getInstance()
          instance.setData(this.multiplier * 5)
          return instance.getData()
        }
      }
      return obj.processSingleton() === 10
    })()
  )

  console.log(
    "15. 复杂对象this绑定:",
    (() => {
      const obj = {
        config: { value: 42 },
        getConfigSingleton: function() {
          const instance = Singleton.getInstance()
          instance.setData(this.config.value)
          return instance.getData()
        }
      }
      return obj.getConfigSingleton() === 42
    })()
  )
}

testSingletonPattern()
